package com.flyoil.petromp.ui.activity.activity_home;

import android.app.Fragment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.cnpc.fyutils.NotificationsUtils;
import com.cnpc.fyviewlibrary.view.NoScrollViewPager;
import com.cnpc.fyviewlibrary.viewpager.FragmentPagerAdapter;
import com.flyoil.petromp.Constant.ConHander;
import com.flyoil.petromp.R;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.ui.activity.activity_login.LoginActivity;
import com.flyoil.petromp.ui.fragment.fragment_home.HomeFragment;
import com.flyoil.petromp.ui.fragment.fragment_statiscs.StatisticsFragment;
import com.flyoil.petromp.ui.fragment.fragment_home.MeFragment;
import com.flyoil.petromp.utils.ApplicationHelp;
import com.flyoil.petromp.utils.Dialog.Dialog_util;
import com.flyoil.petromp.utils.MoccaPreferences;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity {
    private NoScrollViewPager viewPager;
    private FragmentPagerAdapter adapter;
    private List<Fragment> fragments;
    private TextView tv_home, tv_statcs, tv_user;

    private Handler handler = new Handler() {
        @Override
        public String getMessageName(Message message) {
            return super.getMessageName(message);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg != null) {
                switch (msg.what) {
                    case 100://退出APP
                        finish();
                        break;
                }
            }
        }
    };

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initViews() {
        super.initViews();
        viewPager = $(R.id.viewpager_home);
        viewPager.setScrollble(false);
        tv_home = $(R.id.tv_home_home);
        tv_statcs = $(R.id.tv_home_statics);
        tv_user = $(R.id.tv_home_user);
        ApplicationHelp.addHandler(handler, ConHander.HOME_HANDER);
        //判断用户是否设置了签名，如果尚未签名让用户去设置签名
        if (MoccaPreferences.signature_seted.get() == false) {
            Dialog_util.showSetSigantureDialog(mContext);
        }
        if (NotificationsUtils.isNotificationEnabled(this) == false) {
            NotificationsUtils.showAppDetailSettingDialog(this);
        }
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        tv_home.setOnClickListener(onClickListener);
        tv_statcs.setOnClickListener(onClickListener);
        tv_user.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = 0;
            switch (view.getId()) {
                case R.id.tv_home_home:
                    position = 0;
                    break;
                case R.id.tv_home_statics:
                    position = 1;
                    break;
                case R.id.tv_home_user:
                    position = 2;
                    break;
            }
            viewPager.setCurrentItem(position);
            seticon(position);
        }
    };

    @Override
    protected void initdatas() {
        super.initdatas();
        fragments = new ArrayList<Fragment>();
        fragments.add(new HomeFragment());//主页
        fragments.add(new StatisticsFragment());//消息
        fragments.add(new MeFragment());//联系人
        adapter = new FragmentPagerAdapter(getFragmentManager()) {


            @Override
            public android.app.Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        viewPager.setOffscreenPageLimit(fragments.size());
        viewPager.setAdapter(adapter);
        seticon(0);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                seticon(position);
                switch (position) {
                    case 0:
                        view_statusbar.setVisibility(View.GONE);
                        break;
                    case 1:
                        view_statusbar.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        view_statusbar.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                switch (arg0) {
                    case 1://开始滑动
                        break;

                }
            }
        });
        view_statusbar.setVisibility(View.GONE);
    }

    //返回键的监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 改变底部三个按钮图标的方法
     *
     * @param id
     */
    private void seticon(int id) {

        switch (id) {
            case 0:// 首页
                tv_home.setSelected(true);
                tv_statcs.setSelected(false);
                tv_user.setSelected(false);
                break;
            case 1://消息
                tv_home.setSelected(false);
                tv_statcs.setSelected(true);
                tv_user.setSelected(false);
                break;
            case 2:// 联系人
                tv_home.setSelected(false);
                tv_statcs.setSelected(false);
                tv_user.setSelected(true);
                break;

        }
    }
}
